package org.chinarjy.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.chinarjy.api.SkillApi;
import org.chinarjy.dto.*;
import org.chinarjy.entity.Person;
import org.chinarjy.entity.Skill;
import org.chinarjy.mapper.PersonSkillMapper;
import org.chinarjy.mapper.SkillMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@DubboService
@Component
public class SkillService implements SkillApi {
    @Autowired
    SkillMapper skillMapper;


    @Override
    public ResultApi<List<SkillInfo>> getAllSkills(){
        List<SkillInfo> skillInfos = new ArrayList<>();
        for(Skill skill:skillMapper.selectList(null)){
            skillInfos.add(new SkillInfo(skill.getId(),skill.getUid(),skill.getDeptId(),skill.getSkill()));
        }
        return ResultApi.success(skillInfos);
    }


    @Override
    public ResultApi<SkillInfo> updateSkillById(SkillInfo skillInfo){
        Skill skill = new Skill(skillInfo.getId(),skillInfo.getUid(),skillInfo.getDeptId(),skillInfo.getSkill());
        int result = skillMapper.updateById(skill);
        if(result > 0){
            return ResultApi.success(skillInfo);
        }else {
            return ResultApi.error("1","网络延迟，稍后再试......");
        }
    }

    @Override
    public ResultApi addAllPersonToSkill(List<PersonInfo> persons) {
        try {
            for(PersonInfo person:persons){
                Skill skill = new Skill();
                skill.setUid(person.getId());
                skill.setDeptId("1001");
                skill.setSkill("java");
                skillMapper.insert(skill);
            }
            return ResultApi.success();
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultApi.error("1","网络延迟，稍后再试......");
        }
    }

    @Override
    public ResultApi<SkillInfo> deleteAllSkills() {
        skillMapper.deleteAllSkills();
        return ResultApi.success();
    }


}
